package n50_atomic;

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

/**
 * @author fu
 * @date 2022-02-16 21:45
 * 概要：
 * ----------------------------------------------------------------
 * |                                                              |
 * |              字段更新器 的 保护字段 要求 用 volatile            |
 * |                                                             |
 * |=============================================================
 */
public class T8_原子更新器_之字段更新器_保护的是对象属性赋值 {

    public static void main(String[] args) {
        final Student stu = new Student();

        final AtomicReferenceFieldUpdater<Student, String> updater = AtomicReferenceFieldUpdater.newUpdater(Student.class, String.class, "name");

        System.out.println(updater.compareAndSet(stu, null, "张三"));
        System.out.println(stu);
    }

}

class Student {

    volatile String name;

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                '}';
    }
}
